<?php
class Ajax extends CI_Controller{
	
	public function __construct()
    {
        parent::__construct();
		$this->load->model('ajax_m');
    }
	
	
	/* ----------------------------------- Controller Form Login --------------------------------- */
	function index()
	{
		$this->load->view('login_v');
	}
	
	
	
	/* ----------------------------------- Login Proses --------------------------------- */
	
	//Proses
	function akses() {
		$username = $this->input->post('username'); 
		$password = $this->input->post('password'); 
		$pass = md5($password); 
		$q = $this->db->query("SELECT * FROM t_m_user WHERE username='$username' AND password='$pass'");
		if ($q->num_rows() == 1) {
			$nama = $q->row()->username;
			$this->session->set_userdata('username',$nama);
			$data['s_username'] = "$nama";
			
			$level = $q->row()->level;
			$this->session->set_userdata('level',$level);
			$data['s_level'] = "$level";
			
			$this->load->view('themes_top_v', $data);
			$this->load->view('home_v');
			$this->load->view('themes_footer_v');
		}
		else {
			$this->load->view('themes_error_v');
		}
	}
	
	// Logout
	function logout() {
	$this->session->sess_destroy();
	$this->load->view('login_v');
	}
	

	
	/* ----------------------------------- Controller Home --------------------------------- */
	
	function home()
	{
		$this->load->view('themes_top_v');
		$this->load->view('home_v');
		$this->load->view('themes_footer_v');
	}
	
	
	
	/* ----------------------------------- Controller Users --------------------------------- */
	
	//Tampil Users
	function users()
	{
		$data['users']=$this->ajax_m->tampil();
		$this->load->view('themes_top_v');
		$this->load->view('users_v',$data);
		$this->load->view('themes_footer_v');
	}
	
	//create Users
	function create_users()
	{
		$pass=md5($this->input->post('password'));
		$data=array(
			'nama_lengkap' => $this->input->post('nama_lengkap'),
			'username' => $this->input->post('username'),
			'password'=>$pass,
			'no_telp' => $this->input->post('no_telp'),
			'email' => $this->input->post('email'),
			'level' => $this->input->post('level')
		);
		$this->ajax_m->create_users($data);
		redirect('login/users');
	}
	
	// Form Update Users
	function form_update_users($id_user)
	{
		$data['users']=$this->ajax_m->update_users($id_user);
		$this->load->view('themes_top_v');
		$this->load->view('form_update_user_v', $data);
		$this->load->view('themes_footer_v');
	}
	
	// Form Proses Update Users
	function proses_update_users($id_user)
	{
		$data['users']=$this->ajax_m->proses_update_users($id_user);
		redirect('login/users');
	}
	
	// Delete users
	function delete_users()
	{
		$this->ajax_m->delete_users();
		redirect('login/users');
	}
	
	
	/* ----------------------------------- Controller Departemen --------------------------------- */
	// Tampil
	function departemen()
	{
		$data['departemen']=$this->ajax_m->tampil_departemen();
		$this->load->view('themes_top_v');
		$this->load->view('departemen_v',$data);
		$this->load->view('themes_footer_v');
	}
	
	//create departemen
	function create_departemen()
	{
		$data=array(
			'nm_dept' => $this->input->post('nm_dept')
		);
		$this->ajax_m->create_departemen($data);
		redirect('login/departemen');
	}
	
	// Form Update Users
	function form_update_departemen($id_dept)
	{
		$data['departemen']=$this->ajax_m->update_departemen($id_dept);
		$this->load->view('themes_top_v');
		$this->load->view('form_update_departemen_v', $data);
		$this->load->view('themes_footer_v');
	}
	
	// Form Proses Update Departemen
	function proses_update_departemen($id_dept)
	{
		$data['departemen']=$this->ajax_m->proses_update_departemen($id_dept);
		redirect('login/departemen');
	}
	
	// Delete departemen
	function delete_departemen()
	{
		$this->ajax_m->delete_departemen();
		redirect('login/departemen');
	}
	
	
	
	/* ----------------------------------- Controller Jam Kerja --------------------------------- */
	
	//Tampil Jam kerja
	function jam_kerja()
	{
		$data['jam_kerja']=$this->ajax_m->tampil_jam_kerja();
		$this->load->view('themes_top_v');
		$this->load->view('jam_kerja_v',$data);
		$this->load->view('themes_footer_v');
	}
	
	//create Jam Kerja
	function create_jam_kerja()
	{
		$data=array(
			'jam_masuk' => $this->input->post('jam_masuk'),
			'jam_keluar' => $this->input->post('jam_keluar'),
			'b_active' => $this->input->post('b_active')
		);
		$this->ajax_m->create_jam_kerja($data);
		redirect('login/jam_kerja');
	}
	
	// Form Update Jam Kerja
	function form_update_jam_kerja($id)
	{
		$data['jam_kerja']=$this->ajax_m->update_jam_kerja($id);
		$this->load->view('themes_top_v');
		$this->load->view('form_update_jam_kerja_v', $data);
		$this->load->view('themes_footer_v');
	}
	
	// Form Proses Update Jam Kerja
	function proses_update_jam_kerja($id)
	{
		$data['jam_kerja']=$this->ajax_m->proses_update_jam_kerja($id);
		redirect('login/jam_kerja');
	}
	
	// Delete Jam Kerja
	function delete_jam_kerja()
	{
		$this->ajax_m->delete_jam_kerja();
		redirect('login/jam_kerja');
	}
	
	
	
	
	/* ----------------------------------- Controller Karyawan --------------------------------- */
	
	//Tampil Karyawan
	function karyawan()
	{
		$data['departemen']=$this->ajax_m->tampil_departemen();
		$data['karyawan']=$this->ajax_m->tampil_karyawan();
		$this->load->view('themes_top_v');
		$this->load->view('karyawan_v',$data);
		$this->load->view('themes_footer_v');
	}
	
	//create Karyawan
	function create_karyawan()
	{
		$data=array(
			'NIP' => $this->input->post('NIP'),
			'Nama' => $this->input->post('Nama'),
			'Alamat' => $this->input->post('Alamat'),
			'NoTelp' => $this->input->post('NoTelp'),
			'ip' => $this->input->post('ip'),
			'id_dept' => $this->input->post('departemen')
		);
		$this->ajax_m->create_karyawan($data);
		redirect('login/karyawan');
	}
	
	// Form Update Karyawan
	function form_update_karyawan($NIP)
	{
		$data['departemen']=$this->ajax_m->tampil_departemen();
		$data['karyawan']=$this->ajax_m->update_karyawan($NIP);
		$this->load->view('themes_top_v');
		$this->load->view('form_update_karyawan_v', $data);
		$this->load->view('themes_footer_v');
	}
	
	// Delete Karyawan
	function delete_karyawan()
	{
		$this->ajax_m->delete_karyawan();
		redirect('login/karyawan');
	}
	
	/* ----------------------------------- Controller Absensi --------------------------------- */
	
	//Tampil Absensi
	function absensi()
	{
		$data['absensi']=$this->ajax_m->tampil_absensi();
		$this->load->view('themes_top_v');
		$this->load->view('absensi_v',$data);
		$this->load->view('themes_footer_v');
	}
	function js($kd){
		switch($kd){
			case 1:
				$this->load->view('js/ajax');
			break;
			case 2:
				$this->load->view('js/ajax_hr');
			break;
		}		
	}
	
	// metode ajax
	public function getMenu($id,$type){		
		$this->session->set_userdata('menu',$id);
                $this->session->set_userdata('content-type',$type);                
		switch($id){			
			case '0':
				switch($this->session->userdata('level')){
					case 1:					
						$this->load->view('dashboard_hr');
					break;								
					default:
						$this->load->view('dashboard_finance');
					break;
				}
			break;
			case 'hr':				
				echo "Ini modul HR";
			break;
			case 'finance':				
				echo "Ini modul Finance";
			break;			
			case 'purchasing':				
				echo "Ini modul Purchasing";
			break;
			case 'marketing':				
				echo "Ini modul Marketing";
			break;
			case 'warehouse':				
				echo "Ini modul Warehouse";
			break;			
			case 'dept':				
				$data['departemen']=$this->ajax_m->tampil_departemen();				
				$this->load->view('departemen_v',$data);
			break;			
			case 'jam_kerja':
				$data['jam_kerja']=$this->ajax_m->tampil_jam_kerja();
				$this->load->view('jam_kerja_v',$data);
			break;
			case 'karyawan':			
				$data['departemen']=$this->ajax_m->tampil_departemen();
				$data['karyawan']=$this->ajax_m->tampil_karyawan();	
				$this->load->view('karyawan_v',$data);
			break;
				// menu kiri
			case 'absensi':                                
				$data['absensi']=$this->ajax_m->tampil_absensi();				
				$this->load->view('absensi_v',$data);                                                                 
                                 
			break;
			case 'payroll':
				echo "Ini buat payroll..";
			break;	
			default:
				echo "Undermaintenance..";
			break;
		}		
	}
	function updateAbsenToday(){
		$absensi=$this->ajax_m->tampil_absensi();
		?>
		<script language="javascript">
		$(function(){
			$('#id_absen').on('click',function(){
				var val=$(this).attr('val');
				$(this).off();
				alert(val);
			});
		});
		</script>
		<table cellpadding="0" cellspacing="0" border="0" class="table tabe-hover" id="tblAbsensi" style="font-size:12px; margin-bottom:20px;">
		<thead>
			<tr>
				<th>No</th>
				<th>NIP</th>
				<th>IP</th>
				<th>Nama</th>
				<th>Departemen</th>
				<th>Masuk</th>
				<th>Keluar</th>
				<th>Terlambat</th>
				<th>Overtime</th>
				<th>Last Connect</th>
				<th>Network</th>
				<th></th>
			</tr>
		</thead>		
		<tbody>
		<?php $i = 1 ?>
		<?php foreach($absensi as $data){?>
		<tr>
			<td><?php echo $i++ ?></td>
			<td><?php echo $data->nip ?></td>
			<td><?php echo $data->ip ?></td>
			<td><?php echo $data->nama_lengkap ?></td>
			<td><?php echo $data->nm_dept ?></td>
			<td><?php echo $data->masuk ?></td>
			<td><?php echo $data->keluar ?></td>
			<td><?php echo $data->terlambat;?></td>
			<td><?php echo $data->overtime ?></td>
			<td><?php echo $data->last_connect;?></td>
			<td>
				<?php
					$menit=date('i');
					$getMinute=substr($data->last_connect,14,2);										
					if($menit<>$getMinute){
						?>
							<img src="<?php echo base_url();?>assets/img/red.png" width=10 height=10/>
						<?php
					}else{
						?>
							<img src="<?php echo base_url();?>assets/img/success.png"/>
						<?php
					}				
				?>
			</td>
			<td><a href="#" id="id_absen" val="<?php echo $data->id;?>">tes</a></td>
		</tr>
		<?php }?>
		</tbody>
	</table>
	<?php
	}
}
?>